<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Models\Order;
use App\Models  \User;
use App\Http\Resources\Admin\Order as OrderResource;
use App\Http\Resources\Admin\OrderCollection;
use DB;

class OrderController extends ApiController
{
    /**
     * 订单列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $status = $request->input('status', '');
        $limit = $request->input('size', 15);
        $order_no = $request->input('order_no', 0);
        $user_name = $request->input('user_name', '');
        $date = $request->input('date');

        $orm = Order::orderBy('id','DESC');
        $manager = $request->user('manager');
        if (!$manager->isAdmin()) {
            $store = $manager->user->mstore;
            $orm->where('store_id', $store->id);
        }

        if ($status != '') {
            // 根据订单状态查询
            $orm->where('status', $status);
        }

        if($order_no) {
            // 根据订单号查找
            $orm->where('order_no', $order_no);
        }

        if($user_name) {
            // 根据用户查找
            $pattern = '/^1([0-9]{9})/'; 
            if (preg_match($pattern, $user_name)) { 
                // 是手机号
                $userOrm = User::where('telphone', $user_name);
            }else{ 
                // 不是手机号根据昵称筛选
                $userOrm = User::where('name', 'like', '%'.$user_name.'%');
            }

            $user = $userOrm->first();

            if($user){
                $orm->where('user_id', $user->id);
            }else {
                $orm->where('user_id', -1);
            }
        }

        if($date) {
            if($date[0] && time($date[0])) {
                $orm->where('created_at', '>', $date[0]);
            }

            if($date[1] && time($date[1])) {
                $orm->where('created_at', '<', $date[1]);
            }
        }

        $orm->with(['user' => function ($query) {
            $query->select('id', 'name', 'telphone');
        }])->with(['orderItems' => function ($query) {
            $query->select('*');
        }]);

        return new OrderCollection($orm->paginate($limit));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        
    }

    /**
     * 获取指定订单项目
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $orderOrm = Order::with(['user' => function($query) {
            $query->select('id', 'name', 'telphone');
        }])->with(['store' =>function($query) {
            $query->select('id', 'name');
        }])->with(['products']);
        return $this->success($orderOrm->find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 按照订单状态统计
     */
    public function count() {
        $count = Order::groupBy('status')->get([DB::raw('status'), DB::raw('COUNT(*) as count')]);

        $countMap = [];
        $statusMap = [
            0 => 'pending_payment', 
            1 => 'pending_delivery', 
            2 => 'shipped', 
            3 => 'completed',
            7 => 'close',
        ];

        $contAll = 0;
        // foreach ($count as $key => $value) {
        //     $countMap[$statusMap[$value['status']]] = $value['count'];
        //     $contAll += $value['count'];
        // }
        $countMap['all'] = $contAll;
        return $this->success($countMap);
    }
}
